home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / visual / perl.exe / {app} / Plug-Ins / RandomColorCoding.pl < prev    next >
Encoding:
Text File  |  2003-05-30  |  2.3 KB  |  130 lines

  1. #Name: Random Color Coding
  2. #Description: Selects random colors for box & line coding
  3. #Icon: %opti%Tools.icl,147
  4.  
  5. use Win32::OLE;
  6.  
  7. sub Initialization {
  8.  $optiperl = Win32::OLE->new('OptiPerl.Application');
  9.  $plug_id = $_[0];
  10.  
  11.  @RGB = hex2rgb($optiperl -> GetColor("EditorColor"));
  12.  
  13.  if ($RGB[0]>128) {
  14.   $lum = 220; # we have a light background
  15.  }
  16.  else
  17.  {
  18.   $lum = 40;  # we have a dark background
  19.  }
  20.  
  21.  $sat = 235; # (0..240)
  22.  
  23.  sub DoColor {
  24.   $hue=rand(240);
  25.   $color=HSLRangeToRGB($hue,$sat,$lum);
  26.   $optiperl -> SetColor($_[0],$color);
  27.  }
  28.  
  29.  for (my $i=1; $i<=6; $i++)
  30.  {
  31.   DoColor("Line".$i."Color");
  32.   DoColor("BoxBr".$i."Color");
  33.   DoColor("BoxPar".$i."Color");
  34.  }
  35.  
  36.  DoColor("BoxHereDocColor");
  37.  DoColor("BoxPodColor");
  38.  
  39.  $optiperl -> UpdateOptions(1);
  40.  $optiperl->EndPlugIn($plug_id);
  41. }
  42.  
  43. sub Finalization {
  44. }
  45.  
  46. sub HSLRangeToRGB
  47. {
  48.  ($R, $G, $B) =
  49.   HSLtoRGB(($_[0]-1) / (240-1), $_[1] / 240, $_[2] / 240);
  50.  return rgb2hex($R, $G, $B);
  51. }
  52.  
  53.  
  54. sub HSLtoRGB {
  55.  
  56.   sub HueToColourValue {
  57.    my $V;
  58.    my $Hue=$_[0];
  59.    if ($Hue < 0) {
  60.       $Hue = $Hue + 1
  61.    }
  62.     else
  63.    {
  64.       if ($Hue > 1) {
  65.         $Hue = $Hue - 1;
  66.       }
  67.    }
  68.  
  69.    if ((6 * $Hue) < 1) {
  70.       $V = $M1 + ($M2 - $M1) * $Hue * 6;
  71.    }
  72.     else
  73.    {
  74.       if ((2 * $Hue) < 1) {
  75.         $V = $M2
  76.       }
  77.       else
  78.       {
  79.         if ((3 * $Hue) < 2) {
  80.           $V = $M1 + ($M2 - $M1) * (2/3 - $Hue) * 6
  81.         }
  82.         else
  83.         {  $V = $M1; }
  84.       }
  85.     }
  86.     return int (255 * $V);
  87.   }
  88.  
  89.  ($H, $S, $L) = @_;
  90.  if ($S == 0)
  91.  {
  92.     $R = int (255 * $L);
  93.     $G = $R;
  94.     $B = $R;
  95.  }
  96.  else {
  97.     if ($L <= 0.5)
  98.       { $M2 = $L * (1 + $S) }
  99.     else
  100.       { $M2 = $L + $S - $L * $S }
  101.     $M1 = 2 * $L - $M2;
  102.     $R = HueToColourValue ($H + 1/3);
  103.     $G = HueToColourValue ($H);
  104.     $B = HueToColourValue ($H - 1/3)
  105.   }
  106.  
  107.   return ($R, $G, $B);
  108. }
  109.  
  110. sub hex2rgb {
  111.     my $clr = shift;
  112.     my @rgb = $clr =~ /^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
  113.     return unless @rgb;
  114.     return map { hex $_ } @rgb;
  115. }
  116.  
  117. sub rgb2hex {
  118.     return unless @_ == 3;
  119.     my $color = '#';
  120.     foreach my $cc (@_)
  121.     {
  122.         $color .= sprintf("%02x", $cc);
  123.     }
  124.     return $color;
  125. }
  126.  
  127. if (! defined $valid_plugin)
  128. {
  129.  Initialization;
  130. }